home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
ivbsrc
/
testsort.frm
< prev
next >
Wrap
Text File
|
1995-05-08
|
3KB
|
112 lines
VERSION 2.00
Begin Form Form1
Caption = "Test sorted list box"
ClientHeight = 2655
ClientLeft = 1020
ClientTop = 1425
ClientWidth = 3405
Height = 3060
Left = 960
LinkMode = 1 'Source
LinkTopic = "Form1"
ScaleHeight = 2655
ScaleWidth = 3405
Top = 1080
Width = 3525
Begin TextBox Text2
Height = 375
Left = 1560
TabIndex = 4
Top = 2160
Width = 1695
End
Begin CommandButton Command2
Caption = "End"
Height = 495
Left = 1560
TabIndex = 6
Top = 1560
Width = 1695
End
Begin CommandButton Command1
Caption = "Search"
Height = 495
Left = 120
TabIndex = 3
Top = 1560
Width = 1335
End
Begin TextBox Text1
Height = 375
Left = 1560
TabIndex = 1
Top = 1080
Width = 1695
End
Begin ListBox List1
Height = 810
Left = 120
Sorted = -1 'True
TabIndex = 0
Top = 120
Width = 3135
End
Begin Label Label2
Caption = "Search results:"
Height = 255
Left = 120
TabIndex = 5
Top = 2160
Width = 1335
End
Begin Label Label1
Caption = "Search item:"
Height = 255
Left = 120
TabIndex = 2
Top = 1080
Width = 1215
End
End
Function BinarySearch% (Ctrl As Control, Search$, LineNbr%)
If TypeOf Ctrl Is ListBox Then
NbrRecs% = Ctrl.ListCount
Found% = 0 'Item not found yet
LoNbr% = 0
HiNbr% = NbrRecs% - 1
Do
MidNbr% = (LoNbr% + HiNbr%) \ 2
If UCase$(Search$) < UCase$(Ctrl.List(MidNbr%)) Then 'Search the low portion of the list
HiNbr% = MidNbr% - 1
ElseIf UCase$(Search$) > UCase$(Ctrl.List(MidNbr%)) Then 'Search the high portion of the list
LoNbr% = MidNbr% + 1
Else 'Found it
Found% = -1
LineNbr% = MidNbr% 'Return record number
End If
Loop Until Found% Or (HiNbr% < LoNbr%)
BinarySearch% = Found% 'Return success code
End If
End Function
Sub Command1_Click ()
S$ = Text1.Text
If BinarySearch%(list1, S$, L%) Then
Text2.Text = Str$(L%)
Else
Text2.Text = "Not Found"
End If
End Sub
Sub Command2_Click ()
End
End Sub
Sub Form_Load ()
list1.AddItem "ABD"
list1.AddItem "abc"
list1.AddItem "afk"
End Sub